/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.init;

import java.io.IOException;
import java.util.Properties;

import com.eelpo.cmmp.uac.dao.vo.User;
import com.eelpo.cmmp.uac.dao.vo.enumed.AuthMode;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.dao.vo.enumed.YesOrNo;
import com.eelpo.cmmp.uac.services.IUserManageService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * <pre>
 * 
 * 用户初始化
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class UserInit implements ApplicationContextAware {

	private static final Logger logger = LoggerFactory.getLogger(UserInit.class);

	private boolean check;

	public void setCheck(boolean check) {
		this.check = check;
	}

	public boolean isCheck() {
		return check;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext
	 *      (org.springframework.context.ApplicationContext)
	 */
	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		if (isCheck()) {

			// 初始化用户
			try {
				initUser((IUserManageService) context.getBean("userManageService"));
			} catch (Exception e) {
				logger.error("Init user fail:{}", e.getMessage());
			}
		}
	}

	public void initUser(IUserManageService userManageService) throws IOException {

		Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:conf/user-*.properties");

		for (Resource resource : resources) {
			logger.info("Init user from file:{}", resource.getFilename());
			Properties prop = new Properties();
			prop.load(resource.getInputStream());
			String[] configs = prop.getProperty("User").split(",");
			for (String config : configs) {

				boolean exist = false;

				String account = prop.getProperty(config.concat(".account"), config);

				try {
					exist = userManageService.checkExist(account);
				} catch (Exception e) {
					logger.error(e.getMessage());
				}

				if (!exist) {

					User user = new User();
					user.setAccount(account);
					user.setPassword(prop.getProperty(config.concat(".password")));
					user.setName(prop.getProperty(config.concat(".name")));
					user.setMobile(prop.getProperty(config.concat(".mobile")));
					user.setDescription(prop.getProperty(config.concat(".description"), config));
					user.setStatus(Status.valueOf(Byte.valueOf(prop.getProperty(config.concat(".status"), "1"))));
					user.setAllowWebLogin(YesOrNo.valueOf(Boolean.parseBoolean(prop.getProperty(config.concat(".allowWebLogin"), "true"))));
					user.setAllowInterface(YesOrNo.valueOf(Boolean.parseBoolean(prop.getProperty(config.concat(".allowInterface"), "true"))));
					user.setWebAuthMode(AuthMode.valueOf(Byte.valueOf(prop.getProperty(config.concat(".webAuthMode"), "0"))));
					user.setInterfaceAuthMode(AuthMode.valueOf(Byte.valueOf(prop.getProperty(config.concat(".interfaceAuthMode"), "0"))));
					user.setSuperManager(YesOrNo.valueOf(Boolean.parseBoolean(prop.getProperty(config.concat(".superManager"), "true"))));

					try {
						userManageService.addUser(user);
					} catch (Exception e) {
						logger.warn("Init user {} fail:{}", user, e.getMessage());
					}
				}
			}
		}
	}
}
